#include "Blocks/SlabBlock.h"

#include "Managers/BlocksManager.h"
#include "GameSingleton.h"

namespace PocketSurvival
{
	SlabBlock::SlabBlock(int32_t fullBlockIndex) : m_fullBlockIndex(fullBlockIndex)
	{
		addInheritance(this);
		addInheritance((IPaintableBlock*)this);
	}

	bool SlabBlock::isFaceTransparent(int32_t face, int32_t value)
	{
		if (GetIsTop(Terrain::ExtractData(value)))
		{
			return face != 4;
		}
		return face != 5;
	}

    void SlabBlock::getCreativeValues(std::vector<int32_t> &blockVec)
    {
        blockVec.push_back(Terrain::MakeBlockValue(getIndex(), 0, SetColor(0, -1)));
        for(int32_t index = 0; index < 16; ++index)
        {
            blockVec.push_back(Terrain::MakeBlockValue(getIndex(), 0, SetColor(0, index)));
        }
    }

    void SlabBlock::getDropValues(int32_t oldValue, int32_t newValue, int32_t toolLevel, std::vector<BlockDropValue> &dropValues)
    {
		if (Terrain::ExtractContents(newValue) != m_fullBlockIndex)
		{
			int32_t data = Terrain::ExtractData(oldValue);
			int32_t data2 = SetColor(0, GetColor(data));
        	dropValues.emplace_back(Terrain::MakeBlockValue(getIndex(), 0, data2), 1);	
		}
    }

    BlockPlacementData SlabBlock::getPlacementValue(ComponentMiner *componentMiner, int32_t value, const TerrainRaycastResult &raycastResult)
    {
		int32_t num = Terrain::ExtractContents(value);
		int32_t data = Terrain::ExtractData(value);
		int32_t num2 = Terrain::ExtractContents(raycastResult.value);
		int32_t data2 = Terrain::ExtractData(raycastResult.value);
		BlockPlacementData result;
		if (num2 == num && ((GetIsTop(data2) && raycastResult.cellFace.face == 5) || (!GetIsTop(data2) && raycastResult.cellFace.face == 4)))
		{
			int32_t value2 = Terrain::MakeBlockValue(m_fullBlockIndex, 0, 0);
			IPaintableBlock* paintableBlock = dynamic_cast<IPaintableBlock*>(GameSingleton::blocksManager->blocks[m_fullBlockIndex]);
			if (paintableBlock != nullptr)
			{
				int32_t color = GetColor(data);
				value2 = paintableBlock->paint(value2, color);
			}
			CellFace cellFace = raycastResult.cellFace;
			cellFace.point -= CellFace::FaceToPoint3(cellFace.face);

			result.value = value2;
			result.cellFace = cellFace;
			return result;
		}
		bool isTop = (raycastResult.cellFace.face >= 4) ? (raycastResult.cellFace.face == 5) : (raycastResult.hitPoint().posY - raycastResult.cellFace.point.posY > 0.5f);
		result.value = Terrain::MakeBlockValue(getIndex(), 0, SetIsTop(data, isTop));
		result.cellFace = raycastResult.cellFace;
		return result;
    }

    const std::vector<BoundingBox> &SlabBlock::getCustomCollisionBoxes(int32_t value)
    {
        int32_t num = GetIsTop(Terrain::ExtractData(value)) ? 1 : 0;
        return BlockConstans::slabCollisionBoxes[num];
    }
    int32_t SlabBlock::getPaintColor(int32_t value)
    {
        return GetColor(Terrain::ExtractData(value));
    }
    int32_t SlabBlock::paint(int32_t value, int32_t color)
    {
        return Terrain::MakeBlockValue(getIndex(), 0, SetColor(Terrain::ExtractData(value), color));
    }
	
} // namespace PocketSurvival
